﻿using pbvs.Data.Repositories.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using pbvs.Services.ServiceExtensions;
using pbvs.Models;

namespace pbvs.Data.Repositories
{
    public class ContentRepository : IContentRepository 
    {
        private readonly PbvsContext _db;

        public ContentRepository (PbvsContext db)
	    {
            _db = db;
	    }

        public IQueryable<Models.ContentPage> Pages
        {
	        get { return _db.ContentPages; }
        }

        public ContentPage Add(Models.ContentPage page)
        {
            _db.ContentPages.Add(page);
            return page;
        }

        public ContentPage Update(Models.ContentPage page)
        {
            ContentPage dbPage = _db.ContentPages.Find(page.Id);
            if (dbPage != null)
            {
                dbPage.MenuTitle = page.MenuTitle;
                dbPage.Title = page.Title;
                dbPage.Body = page.Body;
                dbPage.Parent = page.Parent;
                dbPage.HideFromMenu = page.HideFromMenu;
                dbPage.PageName = page.PageName;
            }
            else
            {
                throw new Exception(string.Format("No Content Page with id == {0} exists, check value again.", page.Id));
            }
            return dbPage;
        }

        public int Save()
        {
            return _db.SaveChanges();
        }
    }
}